// 导航组件JavaScript
document.addEventListener('DOMContentLoaded', function() {
    // 设置当前页面的导航高亮
    setActiveNavigation();
    
    // 移动端导航菜单切换
    initMobileNavigation();
    
    // 下拉菜单交互
    initDropdownMenus();
});

// 设置当前页面的导航高亮
function setActiveNavigation() {
    const currentPage = window.location.pathname.split('/').pop() || 'index.html';
    const navLinks = document.querySelectorAll('.nav-menu a');
    
    navLinks.forEach(link => {
        link.classList.remove('active');
        const href = link.getAttribute('href');
        if (href === currentPage || (currentPage === '' && href === 'index.html')) {
            link.classList.add('active');
        }
    });
}

// 移动端导航菜单初始化
function initMobileNavigation() {
    const mobileMenuToggle = document.getElementById('mobileMenuToggle');
    const navMenu = document.getElementById('navMenu');
    const body = document.body;
    
    if (mobileMenuToggle && navMenu) {
        // 汉堡菜单点击事件
        mobileMenuToggle.addEventListener('click', function() {
            const isActive = navMenu.classList.contains('active');
            
            if (isActive) {
                // 关闭菜单
                navMenu.classList.remove('active');
                mobileMenuToggle.classList.remove('active');
                body.style.overflow = '';
            } else {
                // 打开菜单
                navMenu.classList.add('active');
                mobileMenuToggle.classList.add('active');
                body.style.overflow = 'hidden'; // 防止背景滚动
            }
        });
        
        // 点击菜单外部关闭菜单
        document.addEventListener('click', function(e) {
            if (!navMenu.contains(e.target) && !mobileMenuToggle.contains(e.target)) {
                navMenu.classList.remove('active');
                mobileMenuToggle.classList.remove('active');
                body.style.overflow = '';
            }
        });
        
        // 窗口大小改变时关闭移动端菜单
        window.addEventListener('resize', function() {
            if (window.innerWidth > 768) {
                navMenu.classList.remove('active');
                mobileMenuToggle.classList.remove('active');
                body.style.overflow = '';
            }
        });
    }
}

// 下拉菜单交互初始化
function initDropdownMenus() {
    const dropdownTriggers = document.querySelectorAll('.has-dropdown');
    
    dropdownTriggers.forEach(trigger => {
        const dropdown = trigger.querySelector('.dropdown-menu');
        const dropdownToggle = trigger.querySelector('.dropdown-toggle');
        
        if (dropdown && dropdownToggle) {
            // 桌面端：鼠标悬停
            if (window.innerWidth > 768) {
                trigger.addEventListener('mouseenter', function() {
                    dropdown.style.opacity = '1';
                    dropdown.style.visibility = 'visible';
                    dropdown.style.transform = 'translateY(0)';
                });
                
                trigger.addEventListener('mouseleave', function() {
                    dropdown.style.opacity = '0';
                    dropdown.style.visibility = 'hidden';
                    dropdown.style.transform = 'translateY(-10px)';
                });
            } else {
                // 移动端：点击切换
                dropdownToggle.addEventListener('click', function(e) {
                    e.preventDefault();
                    trigger.classList.toggle('active');
                });
            }
        }
    });
    
    // 监听窗口大小变化，重新初始化下拉菜单
    window.addEventListener('resize', function() {
        // 清除所有激活状态
        dropdownTriggers.forEach(trigger => {
            trigger.classList.remove('active');
            const dropdown = trigger.querySelector('.dropdown-menu');
            if (dropdown) {
                dropdown.style.opacity = '';
                dropdown.style.visibility = '';
                dropdown.style.transform = '';
            }
        });
        
        // 重新初始化
        setTimeout(() => {
            initDropdownMenus();
        }, 100);
    });
}

// 平滑滚动到页面顶部
function scrollToTop() {
    window.scrollTo({
        top: 0,
        behavior: 'smooth'
    });
}

// 导航栏滚动效果（可选）
function initScrollEffect() {
    let lastScrollTop = 0;
    const nav = document.querySelector('.main-nav');
    
    window.addEventListener('scroll', function() {
        const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
        
        if (scrollTop > lastScrollTop && scrollTop > 100) {
            // 向下滚动时隐藏导航栏
            nav.style.transform = 'translateY(-100%)';
        } else {
            // 向上滚动时显示导航栏
            nav.style.transform = 'translateY(0)';
        }
        
        lastScrollTop = scrollTop;
    });
}

// 如果需要滚动隐藏效果，取消注释下面这行
// initScrollEffect();